

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas" width="500px" height="500px"></canvas>
    <script>
        //开启canvas代码提示
        /** @type {HTMLCanvasElement} */
        // 1.获取画布
        let canvas = document.getElementById('canvas')
        // 2.获取画布的上下文
        let ctx = canvas.getContext('2d')

        ctx.beginPath();
        ctx.moveTo(100, 100)
        ctx.lineTo(100, 300)
        ctx.lineTo(300, 300)
        ctx.lineTo(300, 100)
        ctx.lineTo(100, 100)
        ctx.strokeStyle = 'green'
        ctx.lineWidth = 5
        ctx.stroke()
        ctx.closePath()

    </script>
</body>

</html>